package com.bookcloud.java21.chapter8.创建多个线程84;

// 创建多个线程
public class MultipleThreads {
  public static void main(String[] args) {
    System.out.println("=====主线程执行开始");
    // for循环创建多个线程
    for (int i = 0; i < 5; i++) {
      String threadName = "自定义Thread-" + i;
      new Thread(
              new Runnable() {
                @Override
                public void run() {
                  System.out.println("线程-" + threadName + " 开始运行");

                  // 模拟执行任务
                  for (int j = 0; j < 3; j++) {
                    System.out.println("线程-" + threadName + " 执行任务-" + j);
                    try {
                      Thread.sleep(100);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
                  }
                  System.out.println("线程-" + threadName + " 结束运行");
                }
              },
              threadName)
          .start();
    }
    // 主线程等待一段时间，让其他线程有机会执行
    try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println("主线程结束");
  }
}
